import 'dart:ui';

import '/util/sp/SpUtil.dart';

import 'package:get/get.dart';
import 'lang/en_US.dart';
import 'lang/zh_CN.dart';
import 'log/LogUtil.dart';

class TranslationHelper extends Translations{

  static Locale? get locale => Get.deviceLocale;

  static const fallbackLocale = Locale('en', 'US');


  @override
  Map<String, Map<String, String>> get keys => {
    'zh_CN': zh_CN,
    'en_US': en_US,
  };

  static int language = 0; // 语言下标

  static const List languages = [
    Locale('zh', 'CN'),
    Locale('en', 'US'),
  ];

  // 切换语言
  static void switchLang({int? languageIndex})  {
   int? lang = languageIndex ?? SpUtil.instance.getInt('lang');
   LogUtil.println("语言 $lang");
   if(lang != null){
     Get.updateLocale(languages[lang]);
     language = lang;
     if(languageIndex != null){
       SpUtil.instance.save2Sp('lang', lang);
     }
   }else{
     Locale? locale = Get.deviceLocale;
     LogUtil.println('$locale locale语言');
     String? code = locale?.languageCode;
     LogUtil.println("code ${locale?.languageCode}");
     if(code != null){
       int index;
       if(code.contains("en")){
         index = 1;
       }else{
         index = 0;
       }
       Get.updateLocale(languages[index]);
       language = index;
     }
   }
  }

}